package com.wondersgroup.restbase.service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;

import com.wondersgroup.restbase.dao.BaseDao;
import com.wondersgroup.restbase.dao.BaseDaoImpl;
import com.wondersgroup.restbase.dao.BaseNativeDaoImpl;

/**
 * 提供获取dao方法的抽象服务基类
 * @author wondersgroup
 *
 * @param <T>
 */
@Component
public abstract class DaoSupportService<T> {
	@PersistenceContext
	protected EntityManager outEm;

	/**
	 * 根据实体类型构造对应的dao对象，其中使用的jpa的em仍然受spring托管
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected BaseDao<T> getDao(Class clazz) {
		BaseDao<T> dao = new BaseDaoImpl(clazz) {
			{
				this.em = outEm;
			}
		};
		return dao;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected BaseDao<T> routeDao(String dbName,Class clazz) {
		BaseDao<T> dao = new BaseDaoImpl(clazz) {
			{
				this.em = outEm;
			}
		};
		return dao;
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected BaseDao getNataveDao() {
		BaseDao dao = new BaseNativeDaoImpl() {
			{
				this.em = outEm;
			}
		};
		return dao;
	}
}
